package com.igoal.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.DWRPageDTO;
import com.igoal.dto.FriendDTO;
import com.igoal.dto.UserDTO;
import com.igoal.entity.ComponentEnum;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.LikeService;
import com.igoal.service.UserManagerExt;

@Controller
@RequestMapping("/ajax/userList.html")
public class AjaxUserListController extends BaseAjaxController {

	private String LIKE_LIST = "LIKE";
	private String CHEER_ENTRY_LIST = "CHEER_ENTRY";
	private String FRIEND_LIST = "FRIEND";
	private String USER_GOAL_LIST = "USER_GOAL";

	private LikeService likeService;

	@Autowired
	public void setLikeService(LikeService likeService) {
		this.likeService = likeService;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	/**
	 * Get like list page
	 * total: total page
	 * 
	 * @param modelMap
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap, HttpServletRequest request,
			@RequestParam("type") String type) {

		if (LIKE_LIST.equals(type)) {
			Long comId = Long.parseLong(request.getParameter("componentId"));
			ComponentEnum com = ComponentEnum.valueOf(request.getParameter("component"));
			int pageId = Integer.parseInt(request.getParameter("pageId"));
			int total;
			if (request.getParameter("total") != null) {
				total = Integer.parseInt(request.getParameter("total"));
			} else {
				int totalLike = likeService.likeCount(com, comId);
				total = (totalLike % Constants.LIKE_LIST_PAGE_SIZE == 0) ? totalLike
						/ Constants.LIKE_LIST_PAGE_SIZE : totalLike
						/ Constants.LIKE_LIST_PAGE_SIZE + 1;
			}

			if (total > 0) {
				DWRPageDTO<UserDTO> page = new DWRPageDTO<UserDTO>();
				page.setPageNumber(pageId);
				page.setTotalPage(total);
				List<UserDTO> friendList = likeService.getUsersLikePerPage(com, comId, pageId);
				page.setItems(friendList);
				modelMap.addAttribute("page", page);
				modelMap.addAttribute("componentId", comId);
				modelMap.addAttribute("component", com.toString());
				modelMap.addAttribute("total", total);
			}
			modelMap.addAttribute("type", LIKE_LIST);

		} else if (CHEER_ENTRY_LIST.equals(type)) {
			Long entryId = Long.parseLong(request.getParameter("entryId"));
			Boolean isEntryDone = Boolean.parseBoolean(request.getParameter("isEntryDone"));
			int pageId = Integer.parseInt(request.getParameter("pageId"));
			int total = Integer.parseInt(request.getParameter("total"));

			if (total > 0) {
				DWRPageDTO<FriendDTO> page = new DWRPageDTO<FriendDTO>();
				int totalPage = (total % Constants.LIKE_LIST_PAGE_SIZE == 0) ? total
						/ Constants.LIKE_LIST_PAGE_SIZE : total
						/ Constants.LIKE_LIST_PAGE_SIZE + 1;
				page.setPageNumber(pageId);
				page.setTotalPage(totalPage);
				List<FriendDTO> friendList = goalEntryManager.getEntryCheerPage(entryId,
						isEntryDone, pageId);
				page.setItems(friendList);
				modelMap.addAttribute("page", page);
				modelMap.addAttribute("entryId", entryId);
				modelMap.addAttribute("isEntryDone", isEntryDone.toString());
				modelMap.addAttribute("total", total);
			}
			modelMap.addAttribute("type", CHEER_ENTRY_LIST);

		} else if (FRIEND_LIST.equals(type)) {
			String username = request.getParameter("username");
			int pageId = Integer.parseInt(request.getParameter("pageId"));
			int total = 0;
			if (request.getParameter("total") != null) {
				total = Integer.parseInt(request.getParameter("total"));
			} else {
				total = userManagerExt.countFriends(username);
			}

			if (total > 0) {
				DWRPageDTO<FriendDTO> page = new DWRPageDTO<FriendDTO>();
				int totalPage = (total % Constants.FRIEND_PAGE_SIZE == 0) ? total
						/ Constants.FRIEND_PAGE_SIZE : total / Constants.FRIEND_PAGE_SIZE
						+ 1;
				page.setPageNumber(pageId);
				page.setTotalPage(totalPage);
				List<FriendDTO> friendList = userManagerExt.getFriends(username, pageId);
				page.setItems(friendList);
				modelMap.addAttribute("username", username);
				modelMap.addAttribute("page", page);
				modelMap.addAttribute("total", total);
			}
			modelMap.addAttribute("type", FRIEND_LIST);

		} else if (USER_GOAL_LIST.equals(type)) {
			Long goalId = Long.parseLong(request.getParameter("goalId"));
			int pageId = Integer.parseInt(request.getParameter("pageId"));
			int total = 0;
			if (request.getParameter("total") != null) {
				total = Integer.parseInt(request.getParameter("total"));
			} else {
				total = goalManager.countDoUsers(goalId);
			}

			if (total > 0) {
				DWRPageDTO<UserDTO> page = new DWRPageDTO<UserDTO>();
				int totalPage = (total % Constants.USER_DO_GOAL_PAGE_SIZE == 0) ? total
						/ Constants.USER_DO_GOAL_PAGE_SIZE : total
						/ Constants.USER_DO_GOAL_PAGE_SIZE + 1;
				page.setPageNumber(pageId);
				page.setTotalPage(totalPage);
				List<UserDTO> doUsers = goalManager.getUsersDoGoal(goalId, pageId);
				page.setItems(doUsers);
				modelMap.addAttribute("goalId", goalId);
				modelMap.addAttribute("page", page);
				modelMap.addAttribute("total", total);
			}
			modelMap.addAttribute("type", USER_GOAL_LIST);

		}

		return getViewPrefix() + "ajaxUserList";
	}
}
